package com.fairytask.ui
{
	import com.fairytask.data.Modulator;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.group.ViewGroup;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.window.Window;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.net.CustomToClientEvent;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.item.CardItem;
	import com.greensock.TimelineLite;
	import com.greensock.TweenMax;
	
	import flash.events.MouseEvent;
	
	/**
	 *首充好礼 
	 * @author pangj
	 * 
	 */
	public class FirstPayAwardUI extends FrameUI
	{
		private static var _self    :FirstPayAwardUI=null;
		private var _model           :Modulator = Modulator.instance;
		
		public var beautyIcon        :Clip;      //美女图
		public var depictBG          :Window;    //描述文字底
		public var depictLab         :Label;     //描述文字标签
		public var payPromptly       :Button;    //立即充值按钮
		public var awardIcon         :ViewGroup; //奖品图标
		public var receive           :Button;    //领取按钮
		private var bgTimeLine       :TimelineLite = new TimelineLite({repeat:-1});
		
		public static function getInstance():FirstPayAwardUI
		{
			if(_self==null)
			{
				_self=new FirstPayAwardUI();
			}
			return _self;
		}
		public function FirstPayAwardUI(popUI:Boolean=true)
		{
			super(popUI);
			initIcon();
			initEvent();
			
			bgTimeLine.append(TweenMax.to(receive, 0.5, {glowFilter:{color:0xf58220, alpha:0.7, blurX:20, blurY:20, strength:2, quality:1},repeat:-1,yoyo:true,startAt:{glowFilter:{color:0xFFFFFF, alpha:0.2, blurX:0, blurY:0, strength:1, quality:1}}}));
			bgTimeLine.pause();
		}
		override public function show(space:String=MIDDLELAYER, onshow:Function=null):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SHOWAWAWARD);
			super.show(space,onshow);
			
		}
		
		private function initIcon():void
		{
			for(var i:int=0;i<6;i++)
			{
				var type   : int = int(StaticIni.getIniVar(i+1,"AwardType"));
				var Id     : int = int(StaticIni.getIniVar(i+1,"ItemId"));
				var amount : int = int(StaticIni.getIniVar(i+1,"Amount"));
				setIcon(type,Id,amount,i);
			}
		}
		private function initEvent():void
		{
			_model.addEventListener(CustomToClientEvent.SERVER_COMPLETE_SHOWAWARD,isAward);
			
			awardIcon.addItemsEvent(MouseEvent.MOUSE_OVER,onShowTips);
			awardIcon.addItemsEvent(MouseEvent.MOUSE_OUT,onHideTips);
			receive.addEventListener(MouseEvent.CLICK,onReceive);
			payPromptly.addEventListener(MouseEvent.CLICK,onPay);
		}
		/**
		 * 设置领取奖品按钮状态
		 * @param evt
		 * 
		 */
		private function isAward(evt:CustomToClientEvent):void
		{
			if(true)
			{
				receive.enabled = true;
				bgTimeLine.play();
			}
			else
			{
				receive.enabled = false;
				receive.filters = null;
				bgTimeLine.stop();
			}
		}
		/**
		 *设置奖品图标 
		 * @param type   奖品类型  0:物品 1:银两  2:灵
		 * @param itemID 奖品ID
		 * @param amount 奖品数量
		 * @param pos    
		 * 
		 */
		private function setIcon(type:int,itemID:int,amount:int,pos:int):void
		{
			var tmpItem	:CardItem;
			if(null == awardIcon.getItemByIndex(pos).getItem())
			{
				tmpItem = new CardItem();
				awardIcon.getItemByIndex(pos).addItem(tmpItem);
			}
			else
			{
				tmpItem = awardIcon.getItemByIndex(pos).getItem() as CardItem;
			}
			tmpItem.setData(type,itemID,amount);
		}
		
		/**
		 *显示奖品tips 
		 * @param evt
		 * @param pos
		 * 
		 */
		private function onShowTips(evt:MouseEvent,pos:int):void
		{
			if(null == evt.currentTarget.getItem())
				return;
			if(!(awardIcon.getItemByIndex(pos).getItem() as CardItem).isMask)
			{
				if((awardIcon.getItemByIndex(pos).getItem() as CardItem).isItem)
				{
					var configID:int = (awardIcon.getItemByIndex(pos).getItem() as CardItem).configID;
					var itemType:String = StaticIni.getIniVar(configID,"ItemType");
					TipsManage.ShowAllItemsTips(int(itemType),configID);
				}
				else
				{
					var type	:int = (awardIcon.getItemByIndex(pos).getItem() as CardItem).moneyType;
					switch(type)
					{
						case CardItem.SILVER:
							TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000538",[(awardIcon.getItemByIndex(pos).getItem() as CardItem).moneyVal]));
							break;
						case CardItem.SPIRIT:
							TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000540",[(awardIcon.getItemByIndex(pos).getItem() as CardItem).moneyVal]));
							break;
						case CardItem.ENERGY:
							TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000541",[(awardIcon.getItemByIndex(pos).getItem() as CardItem).moneyVal]));
							break;
					}
				}
			}
		}
		/**
		 *撤销奖品tips 
		 * @param evt
		 * @param pos
		 * 
		 */
		private function onHideTips(evt:MouseEvent,pos:int):void
		{
			ToolTips.hide();
		}
			
		/**
		 *点击领取奖励 
		 * @param evt
		 */
		private function onReceive(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_FIRSTINCRMB);
			this.hide();
		}
		private function onPay(evt:MouseEvent):void
		{
			//跳转到充值页面
		}
	}
}